<?php

namespace Admin\Controller;
use Think\Controller;

class RoleController extends Controller{

	/*
		新增角色
	*/
	public function add(){

		$this->isAjaxLogin();//检查是否登录


		$name=I('post.name');//角色名

		$data['name']   = $name;
       

		$model=D('role');


        if( $model->create($data)){
            $model->add($data); 
        	$ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
        	$ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  "角色添加失败";
        }
        
        $this->ajaxReturn($ajaxReturn);
	}

	/*
	删除角色
	*/

	public function delete(){

		$this->isAjaxLogin();//检查是否登录

        $id = I('post.id/d');

        $model = M('role');

        $model->where('id='.$id)->delete();
        
        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);
	}

	/*
	修改角色
	*/
	public function update(){
		$this->isAjaxLogin();//检查是否登录

		$model=D('role');

		$name=I('post.name');//角色名
		

		$id=I('post.id');

		$data['name']   = $name;
        // var_dump($data);die;
        
        if($model->create($data) &&$model->where('id='.$id)->save($data)){
        	$ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{
        	$ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] =  "角色修改失败";
        }
        $this->ajaxReturn($ajaxReturn);
	}

	/*
	角色列表
	*/

	public function lists(){
		$this->isAjaxLogin();//检查是否登录

        $model = M('role');

        $result = $model->order('id desc')->select();

        if($result !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }
        $this->ajaxReturn($ajaxReturn);
	}
}